package com.example.nio.security.config;

import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONUtil;
import com.example.nio.config.RedisConfig;
import com.example.nio.security.dmo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * token验证处理
 *
 * @author taixin
 */
@Component
public class TokenService {

    @Autowired
    private RedisConfig redisConfig;

    public String createToken(SysUser user){
        String token = UUID.fastUUID().toString();
        user.setToken(token);
        // 保存或更新用户token
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("access_token", token);
        map.put("expires_in", 7200);
        redisConfig.setNx(token,JSONUtil.toJsonStr(user),7200);
        return token;
    }

    public Boolean loginOut(String userName){
        return redisConfig.removeKey(userName);
    }

}